grammar Calc;

prog
  : (expr NEWLINE {System.out.println("=" + $expr.value);})+ EOF
  ;
  
expr returns [double value]
  : term {$value += $term.value;} 
    ('+' T=expr {$value += $T.value;})?
  | 'sin' T=expr {$value = Math.sin($T.value);}
  | 'cos' T=expr {$value = Math.cos($T.value);}
  | 'pi'  {$value = Math.PI;}
  | 'pow' X=expr Y=expr {$value = Math.pow($X.value, $Y.value);}
  | 'mult' {$value=1;} X=term Y=term {$value = $X.value * $Y.value;}
  ;
  
term returns [double value]
  : Number
    {
      $value = Double.parseDouble($Number.text);
    }
  | '(' expr ')'
    {
      $value = $expr.value;
    }
  ;

Number 
  : ('0' .. '9')+ ('.' ('0' .. '9')+)? 
  ;
       
WHITESPACE 
  : (' ' | '\t')+
    {this.skip();}
  ;
  
NEWLINE : '\n';